﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;

namespace translateToTDCallBackFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader objReader = new StreamReader(@"C:\Users\RCYang\Desktop\dll.txt");
            ArrayList lines = new ArrayList();
            string sLine = "";
            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                if (sLine != null)
                {
                    if (sLine.Contains("///"))  continue;
                    if (sLine.Contains("[")) continue;
                    if (sLine.Length < 2) continue;
                    if (sLine.Contains("extern")) continue;

                    sLine = sLine.Trim();
                    int leftParIndex = sLine.IndexOf('(');
                    int rightParIndex = sLine.IndexOf(')');
                    string sig = sLine.Substring(leftParIndex+1,rightParIndex-leftParIndex-1);
                    string name = sLine.Substring(0, leftParIndex);
                    name = name.Replace("public delegate void ", "");
                    string eventName = name + "Event";
                    

                    string line1 =  "\t\tcallback_" + name + "=delegate(" + sig + ")";
                    string line2 = "\t\t{";
                    string line3 = "\t\t\tif (" + eventName+" != null)";
                    string line4 = "\t\t\t{";

                    string line5 = "\t\t\t\t" + eventName + "(this, new " + name + "Args(";
                    string[] sigArr = sig.Split(',');
                    for (int i = 0; i < sigArr.Length; i++)
                    {
                        string temp = sigArr[i].Trim();
                        if (temp.Contains("ref"))
                        {
                            string[] tempArr = temp.Split(' ');
                            line5 = line5 + tempArr[tempArr.Length-1];
                        }
                        else
                        {
                            temp = temp.Replace("int ", "");
                            temp = temp.Replace("bool ", "");
                            line5 = line5 + temp;
                        }
                        if (i < sigArr.Length - 1) line5 = line5 + ",";
                    }
                       
                    line5 = line5 + "));";
                    
                    string line6 = "\t\t\t}";
                    string line7 = "\t\t};";

                    lines.Add(line1);
                    lines.Add(line2);
                    lines.Add(line3);
                    lines.Add(line4);
                    lines.Add(line5);
                    lines.Add(line6);
                    lines.Add(line7);
                }
            }
            objReader.Close();
            //将文件导出到txt
            FileStream fs = new FileStream(@"C:\Users\RCYang\Desktop\dllcallback.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            foreach (string a in lines)
            {
                sw.WriteLine(a);
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }
    }
}
